package com.shopoo.behavior.app;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shopoo.behavior.api.LikeService;
import com.shopoo.behavior.app.converter.AppConverter;
import com.shopoo.behavior.dto.clientobject.BehaviorLikeCO;
import com.shopoo.behavior.dto.cqe.BehaviorLike;
import com.shopoo.behavior.dto.cqe.BehaviorLikeAddCmd;
import com.shopoo.behavior.dto.cqe.BehaviorLikeCancelCmd;
import com.shopoo.behavior.infrastructure.database.dataobject.TBehaviorLike;
import com.shopoo.behavior.infrastructure.database.mapper.LikeMapper;
import com.shopoo.flyer.api.PromotionService;
import com.szmengran.cola.dto.SingleResponse;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Description: 点赞服务
 * @Package com.szmengran.behavior.service.impl
 * @date Mar 4, 2019 12:57:44 PM
 * @author <a href="mailto:android_li@sina.cn">Joe</a>
 */
@Slf4j
@Service
@DubboService
public class LikeServiceImpl implements LikeService {

    private static final ExecutorService executor = Executors.newFixedThreadPool(100);

    @DubboReference
    private PromotionService promotionService;

    @Resource
    private LikeMapper likeMapper;

    @Override
    public SingleResponse<Boolean> like(BehaviorLikeAddCmd behaviorLikeAddCmd) {
        TBehaviorLike tBehaviorLike = AppConverter.INSTANCE.toTBehaviorLike(behaviorLikeAddCmd);
        Boolean flag = likeMapper.insert(tBehaviorLike) > 0;
        if (flag) {
            executor.submit(() -> {
                try {
                    promotionService.addLike(behaviorLikeAddCmd.getPromotionid());
                } catch (Exception e) {
                    log.error("点赞记录增加失败:{}", e);
                }
            });
            return SingleResponse.of(true);
        }
        return SingleResponse.of(false);
    }

    @Transactional
    @Override
    public SingleResponse<Boolean> cancel(BehaviorLikeCancelCmd behaviorLikeCancelCmd) {
        LambdaQueryWrapper<TBehaviorLike> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TBehaviorLike::getPromotionid, behaviorLikeCancelCmd.getPromotionid());
        queryWrapper.eq(TBehaviorLike::getOpenid, behaviorLikeCancelCmd.getOpenid());
        queryWrapper.eq(TBehaviorLike::getShareuserid, behaviorLikeCancelCmd.getShareuserid());

        boolean flag = likeMapper.delete(queryWrapper) == 1;
        if (flag) {
            executor.submit(() -> {
                try {
                    promotionService.cancelLike(behaviorLikeCancelCmd.getPromotionid());
                } catch (Exception e) {
                    log.error("取消点赞失败：{}", e.getMessage());
                }
            });
        }
        return SingleResponse.of(flag);
    }

    @Override
    public SingleResponse<BehaviorLikeCO> findById(BehaviorLike behaviorLike) {
        LambdaQueryWrapper<TBehaviorLike> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TBehaviorLike::getPromotionid, behaviorLike.getPromotionid());
        queryWrapper.eq(TBehaviorLike::getOpenid, behaviorLike.getOpenid());
        queryWrapper.eq(TBehaviorLike::getShareuserid, behaviorLike.getShareuserid());
        TBehaviorLike tBehaviorLike = likeMapper.selectOne(queryWrapper);
        return SingleResponse.of(AppConverter.INSTANCE.toBehaviorLikeCO(tBehaviorLike));
    }

    @Override
    public SingleResponse<Boolean> checkLike(BehaviorLike behaviorLike) {
        boolean flag = likeMapper.checkLike(behaviorLike.getPromotionid(), behaviorLike.getOpenid()) > 0;
        return SingleResponse.of(flag);
    }

    @Override
    public SingleResponse<Integer> getLike(String promotionid, String shareuserid) {
        int count = likeMapper.getLike(promotionid, shareuserid);
        return SingleResponse.of(count);
    }
}
